package tr.com.netgis;

import java.util.Date;

import tr.com.netgis.util.Constants;
import tr.com.netgis.util.LocalMessageType;
import tr.com.netgis.web.SendMessageRequest;
import tr.com.netgis.web.SetLocationResponse;
import tr.com.netgis.web.WebServisCaller;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * 
 * @author NetGis
 *
 */
public class LocalMessageViewActivity extends Activity {

	private static final String TAG = "LocalMessageActivity";
	
	private Button btnSendMessage;
	private EditText txtMessage;
	private ImageView imgType;
	private LocalMessageType messageType;

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_local_message);
		
		btnSendMessage = (Button)findViewById(R.id.ng_new_local_message_btnSend);
		txtMessage = (EditText)findViewById(R.id.ng_new_local_message_txtMessage);
		imgType = (ImageView)findViewById(R.id.newLocalMessageImgType);
		btnSendMessage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				new SendTask().execute();
			}
		});
		
		TextWatcher tw = new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				btnSendMessage.setEnabled(s.length() > 0);
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				if (s.length() > 100) {
					String message = getResources().getString(R.string.msgMessageTextMaxLength, new Object[]{Constants.MAX_LENGTH_MESSAGE_TEXT_SIZE});
					Toast.makeText(LocalMessageViewActivity.this, message, Toast.LENGTH_SHORT);
					
				}
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				btnSendMessage.setEnabled(s.length() > 0);
			}
		};
		txtMessage.addTextChangedListener(tw);
		
		Bundle bundle = getIntent().getExtras();
		messageType = (LocalMessageType) (bundle != null ? bundle.getSerializable("type") : LocalMessageType.TEXT);
		
		if (LocalMessageType.ALARM.equals(messageType)) {
			imgType.setImageResource(R.drawable.ic_alarm);			
		} else if (LocalMessageType.TEXT.equals(messageType)) {
			imgType.setImageResource(R.drawable.ic_text);					
		}
		
	}
	
	ProgressDialog dialog;

	private void dismissProgress() {
		dialog.dismiss();
	}
	
	class SendTask extends AsyncTask<Void, Void, String> {

		@Override
		protected void onPreExecute() {
			String waitMsg = getResources().getString(R.string.msgSendMessageWait);
			dialog = ProgressDialog.show(LocalMessageViewActivity.this, "", waitMsg, true);
			super.onPreExecute();
		}

		protected String doInBackground(Void... vs) {
			SendMessageRequest request = new SendMessageRequest();
			double lat = 99999999, lon = 99999999; 
			/*
			NGLocation lastNGLoc = SetLocationService.getLastNGLocation();
			if (lastNGLoc != null) {
				lat = lastNGLoc.getLat();
				lon = lastNGLoc.getLon();
			} else {
				Location lastLoc = SetLocationService.getLastLocation();
				if (lastLoc != null) {
					lat = lastLoc.getLatitude();
					lon = lastLoc.getLongitude();
				}
			}
			*/
			request.setLastLocationX(lat);
			request.setLastLocationY(lon);
			request.setTarihSaat(new Date());
			request.setMessageType(messageType);
			if (messageType.equals(LocalMessageType.ALARM)) {
				request.setStatus(tr.com.netgis.util.Status.ALARM);
			} else {
				request.setStatus(tr.com.netgis.util.Status.UNKNOWN);				
			}
			request.setMessageBody(txtMessage.getText().toString());
			SetLocationResponse response =  WebServisCaller.callWebService(request);
			if (response.isSuccessful()) {
				return getResources().getString(R.string.msgMessageSendSuccess);
			} else {
				return response.toString();
			}
		}

		protected void onPostExecute(String result) {
			Toast toast = Toast.makeText(NGApplication.getAppContext(), result, 3000);
			toast.show();
			dismissProgress();
			LocalMessageViewActivity.this.finish();
		}
	};	
	
}
